/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UTILS;

import DTO.LoaiDaiLyDTO;
import DTO.QuanDTO;
import DTO.TinhDTO;
import javax.swing.table.TableModel;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author HuuTri
 */
public class TableGUI extends javax.swing.JPanel {

    /**
     * Creates new form TableGUI
     */
    public TableGUI() {
        initComponents();
    }
    
    /**
     * Contructor of TableGUI with param name of Table of GUI
     * @param tableName 
     */
    public TableGUI(TableModel tableModel) {
        initComponents();
        initTable(tableModel);
    }
    
    /**
     * set model data of table
     * @param tableModel 
     */
    public void setTableModel(TableModel tableModel) {
        tableData.setModel(tableModel);
    }
    
    /**
     * get table object
     * @return 
     */
    public JXTable getTable() {
        return tableData;
    }
    
    /**
     * set table object
     * @param table 
     */
    public void setTable(JXTable table) {
        tableData = table;
    }
    
    public Object getSelectedItem(String objectName) {
        int index = -1; 
        switch (objectName) {
            case TinhDTO.MA_TINH: index = 1;
            case LoaiDaiLyDTO.MA_LOAI_DAI_LY: index = 1;
            case QuanDTO.MA_QUAN: index = 1;
        }
        
        return getValueAt(index);
    }
    
    public Object getValueAt(int index) {
        if (index == -1) 
            return "";
        else 
            return tableData.getValueAt(tableData.getSelectedRow(), index);
    }
    /**
     * init table content
     * @param tableModel 
     */
    private void initTable(TableModel tableModel) {
        tableData.setModel(tableModel);
    }
    
    /**
     * set model to tableModel
     * @param model 
     */
    private void setTableDataModel(TableModel model) {
        tableData.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableData = new org.jdesktop.swingx.JXTable();

        tableData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableData.setEditable(false);
        jScrollPane1.setViewportView(tableData);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tableData;
    // End of variables declaration//GEN-END:variables
}
